#include "qdriverdatawidget.h"

#include "qdrivermessagetextedit.h"

#include "../../libs/core/qbaseitemdelegate.h"
#include "../../libs/core/minisplitter.h"
#include "../../libs/device/driver_listern/qserverdriverlistern.h"
#include "../../libs/shared/xmlobject.h"

#include <QHeaderView>
#include <QVBoxLayout>
#include <QScrollBar>
#include <QTextBlock>

QDriverDataWidget::QDriverDataWidget(QWidget *parent) :
    QWidget(parent),
    m_property_view(new QTreeWidget),
    m_textedit(new QDriverMessageTextEdit),
    m_run(true)
{
    m_property_view->setItemDelegate(new QBaseItemDelegate);
    m_property_view->setHeaderLabels(QStringList()<<tr("Name")<<tr("Value"));
    m_property_view->setRootIsDecorated(false);
    m_property_view->setAlternatingRowColors(true);
    m_property_view->header()->setMovable(false);
    m_property_view->setFrameStyle(QFrame::NoFrame);

    MiniSplitter *s=new MiniSplitter;

    s->addWidget(m_property_view);
    s->addWidget(m_textedit);

    s->setStretchFactor(0,0);
    s->setStretchFactor(1,1);

    m_textedit->setFrameStyle(QFrame::NoFrame);
    m_textedit->setReadOnly(true);
    QFont f=m_textedit->font();
    f.setPointSize(10);
    m_textedit->setFont(f);

    QVBoxLayout *v=new QVBoxLayout;
    v->setMargin(0);
    v->setSpacing(0);

    v->addWidget(s);

    setLayout(v);

    connect(m_textedit,SIGNAL(run()),this,SLOT(start()));
}

void QDriverDataWidget::set_property(const QDriverInfo &info)
{
    int c=info.m_propertys.size();
    tagDriverProperty p;
    for(int i=1;i<c;i++)
    {
        p=info.m_propertys.at(i);
        QTreeWidgetItem *item=new QTreeWidgetItem(m_property_view);
        item->setText(0,p.m_name);
        item->setText(1,p.m_value);
        item->setToolTip(1,p.m_value);
    }
}

void QDriverDataWidget::add_data(XMLObject *xml)
{
    if(!m_run)
    {
        return ;
    }
    m_textedit->moveCursor(QTextCursor::End);
    QString str=m_textedit->toPlainText();
    if(!str.endsWith("\n") && str.size()>0)
    {
        m_textedit->insertPlainText("\n");
    }


    int type=xml->get_property("type").toInt();

    QTextCursor cursor=m_textedit->textCursor();
    cursor.movePosition(QTextCursor::End);
    QTextCharFormat format;
    format.setForeground(QColor(40,40,40));
    cursor.insertText(type==1?"WRITE":"READ ",format);

    format.setForeground(QColor(0,0,0));
    m_textedit->insertPlainText("  ");
    cursor.insertText(xml->get_property("time"),format);

    m_textedit->insertPlainText("  ");
    format.setForeground(type==1?QColor(255,0,0):QColor(128,0,0));
    str=xml->get_property("message");
    QString tip;
    int p=0;
    while(p<str.length())
    {
        tip+=str.mid(p,24);
        tip+="\n";
        p+=24;
    }
    tip=tip.left(tip.size()-1);
    format.setToolTip(tip);
    cursor.insertText(str,format);
    m_textedit->moveCursor(QTextCursor::End);

    QTextDocument *doc=m_textedit->document();

    while(doc->blockCount()>1000)
    {
        QTextCursor cursor=m_textedit->textCursor();
        cursor.movePosition(QTextCursor::Start);
        cursor.select(QTextCursor::BlockUnderCursor);
        cursor.removeSelectedText();
        cursor.deleteChar();
        cursor.movePosition(QTextCursor::End);
        m_textedit->setTextCursor(cursor);
    }
}

void QDriverDataWidget::start()
{
    m_run=!m_run;
}
